<template>
  <div class="user-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-form-item>
          <el-button type="primary" @click="handleDownload">
            导出 Excel
          </el-button>
        </el-form-item>
        <!-- <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          添加
        </el-button> -->
        <!-- <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
          批量删除
        </el-button> -->
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.info"
              clearable
              placeholder="请输入客户名称\昵称\手机号"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <!-- <el-table-column align="center" show-overflow-tooltip type="selection" /> -->
      <!-- <el-table-column
        align="center"
        label="ID"
        prop="id"
        show-overflow-tooltip
      /> -->
      <el-table-column
        align="center"
        label="客户名称"
        prop="name"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        label="昵称"
        prop="nickname"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="手机号"
        prop="mobile"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="邮箱"
        prop="email"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="微信号"
        prop="weixin"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="openid"
        prop="openid"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="公司名称"
        prop="company"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="职位"
        prop="position"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="注册账户"
        prop="is_login_alias"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column
        align="center"
        label="创建时间"
        prop="create_time"
        show-overflow-tooltip
      />
      <!-- <el-table-column
        align="center"
        label="操作"
        show-overflow-tooltip
        width="150"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">详情</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column> -->
      <template #empty>
        <el-empty class="vab-data-empty" description="暂无数据" />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { useRouter } from 'vue-router'
  import { defineComponent, onMounted, reactive, toRefs } from 'vue'
  import { getList, exportList } from '@/api/customer'
  export default defineComponent({
    name: 'CustomerManagement',
    setup() {
      const router = useRouter()

      const state = reactive({
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          info: '',
        },
        roleList: [],
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row) => {
        if (row.id) {
          router.push({ path: '/metting/edit', query: { id: row.id } })
        } else {
          router.push('/metting/create')
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total, role },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.roleList = role
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
      })

      return {
        ...toRefs(state),
        setSelectRows,
        handleEdit,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
      }
    },
    methods: {
      // exportData() {
      //   return await getList(state.queryForm)
      // },
      handleDownload: async function () {
        this.downloadLoading = true
        let exportData = await exportList(this.queryForm)

        import('@/utils/excel').then((excel) => {
          const tHeader = [
            '名称',
            '昵称',
            '手机号',
            '邮箱',
            '微信号',
            'openid',
            '公司名称',
            '职位',
            '是否注册账户',
            '创建时间',
          ]
          const filterVal = [
            'name',
            'nickname',
            'mobile',
            'email',
            'weixin',
            'openid',
            'company',
            'position',
            'is_login_alias',
            'create_time',
          ]
          const list = exportData.data.list
          const data = this.formatJson(filterVal, list)
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: '客户信息表',
            autoWidth: true,
            bookType: 'xlsx',
          })
          this.downloadLoading = false
        })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map((v) =>
          filterVal.map((j) => {
            return v[j]
          })
        )
      },
    },
  })
</script>
